package com.fqgj.sentry.biz.mc;

import com.fqgj.sentry.manage.entity.McMenuEntity;
import com.fqgj.sentry.manage.entity.McPermissionEntity;
import com.fqgj.sentry.manage.service.MenuService;
import com.fqgj.sentry.manage.service.PermissionService;
import com.fqgj.sentry.biz.mc.vo.role.MenuPermissionInfo;
import com.fqgj.sentry.biz.mc.vo.role.MenuPermissionVo;
import com.fqgj.sentry.biz.mc.vo.role.PermissionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liupengpeng on 2017/7/16.
 */
@Service
public class PermissionBusiness {
    @Autowired
    private MenuService menuService;
    @Autowired
    private PermissionService permissionService;


    /**
     * 查询菜单权限层级关系
     *
     * @return
     */
    public List<MenuPermissionInfo> findMenuPermissionInfo() {
        List<MenuPermissionInfo> menuPermissionInfoList = new ArrayList<>();
        //查询全部主菜单
        List<McMenuEntity> parentMenuList = menuService.findByPid(0L);
        for (McMenuEntity parentMenu : parentMenuList) {
            MenuPermissionInfo menuPermissionInfo = new MenuPermissionInfo();
            List<MenuPermissionVo> menuPermissionVoList = new ArrayList<>();
            //查询子菜单
            List<McMenuEntity> childMenuList = menuService.findByPid(parentMenu.getId());
            for (McMenuEntity childMenu : childMenuList) {
                MenuPermissionVo menuPermissionVo = new MenuPermissionVo();
                //查询  该菜单下权限
                List<McPermissionEntity> permissionEntityList = permissionService.findListByMenuId(childMenu.getId());
                List<PermissionVo> permissionVos = new ArrayList<>();
                for (McPermissionEntity permissionEntity : permissionEntityList) {
                    PermissionVo permissionVo = new PermissionVo();
                    permissionVo.setPermission(permissionEntity.getPermission())
                            .setPermissionId(permissionEntity.getId())
                            .setDescription(permissionEntity.getDescription());
                    permissionVos.add(permissionVo);
                }
                menuPermissionVo.setMenuName(childMenu.getMenu())
                        .setMenuId(childMenu.getId())
                        .setPermissionList(permissionVos);
                menuPermissionVoList.add(menuPermissionVo);
            }
            menuPermissionInfo.setMenuName(parentMenu.getMenu())
                    .setMenuId(parentMenu.getId())
                    .setChildMenuList(menuPermissionVoList);

            menuPermissionInfoList.add(menuPermissionInfo);
        }
        return menuPermissionInfoList;
    }

}
